package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xubeizhi.common.ResponseResult;
import com.xubeizhi.common.SqlConf;
import com.xubeizhi.entity.JobLog;
import com.xubeizhi.mapper.JobLogMapper;
import com.xubeizhi.service.JobLogService;
import com.xubeizhi.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mi
 * @create 2023/3/9 14:54
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class JobLogServiceImpl extends ServiceImpl<JobLogMapper, JobLog> implements JobLogService {
    @Override
    public ResponseResult listJobLog(String jobName, String jobGroup, String status, String startTime, String endTime, Long jobId) {
        QueryWrapper<JobLog> queryWrapper = new QueryWrapper<JobLog>()
                .orderByDesc(SqlConf.CREATE_TIME).eq(jobId != null,SqlConf.JOB_ID,jobId)
                .like(StringUtils.isNotBlank(jobName),SqlConf.JOB_NAME,jobName)
                .like(StringUtils.isNotBlank(jobGroup),SqlConf.JOB_GROUP,jobGroup)
                .eq(StringUtils.isNotBlank(status),SqlConf.STATUS,status)
                .between(StringUtils.isNotBlank(startTime),SqlConf.START_TIME,startTime,endTime);
        Page<JobLog> page = baseMapper.selectPage(new Page<>(PageUtils.getPageNo(),PageUtils.getPageSize()),queryWrapper);
        return ResponseResult.success(page);
    }

    /**
     * 批量删除日志
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteBatch(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult clean() {
        baseMapper.clean();
        return ResponseResult.success();
    }
}
